<?
$title = "User List";
$pageid = "4";
$accessid = ",a10,";

if (isset($_POST['btnsubmit'])) {
    require("required/dbconnect.php");

    $main_cat = $_POST['main_cat'];
    $name_eng = $_POST['name_eng'];
    $is_publish = $_POST['is_publish'];
    $clean_url = str_replace(" ", "-", $name_eng);

    $chk = mysql_query("select count(*) as result from os_sub_category where main_category='$main_cat' and name_eng='$name_eng'");
    if (mysql_result($chk, 0, "result") > 0) {
        header("Location:subcat_management.php?action=4");
    } else {
        $insert = mysql_query("insert into os_sub_category(main_category,name_eng,is_publish,clean_url)value('$main_cat','$name_eng','is_publish','$clean_url')") or die(mysql_error());
        header("Location:subcat_management.php?action=1");
    }
}

require("required/dbconnect.php");
require("required/function.php");
require("required/header.php");

checkAccess("allow_product_category", 1);
?>
<script src="required/js/jquery-1.4.2.js"></script>
<script src="required/js/common_javascript.js"></script>
<script src="required/js/goAjax.js"></script>
<script>
    function checkExist() {
        var value = document.getElementById("name_eng").value;
        var maincat = document.getElementById("main_cat").value;
        if(value!=""){
			
            changeContent('required/js/ajaxCheckSubCatExist.php?mainCat='+maincat+'&value='+value,"divCatName")
        }
    }
</script>
<?
if (!empty($_GET['action'])) {
    switch ($_GET['action']) {
        case 1:
            $msg = "<div class='dw_success'>Data inserted</div>";
            break;
        case 2:
            $msg = "<div class='dw_success'>Data Deleted</div>";
            break;
        case 3:
            $msg = "<div class='dw_success'>Data Updated</div>";
            break;
        case 4:
            $msg = "<div class='dw_failed'>Category Name already exist, insert failed!</div>";
            break;
        default:
            $msg = "";
            break;
    }
    echo "<script>displayMsg('divMsg')</script>";
} else {
    $msg = "";
}




$get_category = mysql_query("select * from os_sub_category order by main_category desc");
?>
<table width="100%"  cellpadding="0" cellspacing="5" height="300">
    <tr>


        <td valign="top">
            <div class="title">Product Sub Category</div>
            <div id="divMsg"><?= $msg ?></div>
            <br />

            <table width="100%" class="solidborder">
                <tr class="table_header">
                    <td width="3%">No</td><td width="28%">Name</td><td width="22%">Main Category</td><td width="10%">Display</td><td width="16%">Action</td>
                </tr>
                <? if (mysql_num_rows($get_category) == 0) { ?>
                    <tr valign="top">
                        <td align="center" colspan="7">
                            - No record found - 
                        </td>

                        <?
                    } else {
                        $no = 1;
                        while ($a = mysql_fetch_array($get_category, MYSQL_ASSOC)) {
                            $id = $a['id'];
                            if ($no % 2 == 0) {
                                $class1 = "table_item_even";
                                $class2 = "table_item_odd";
                            } else {
                                $class1 = "table_item_odd";
                                $class2 = "table_item_even";
                            }

                            $get_cat = mysql_query("select * from os_category where id=" . $a['main_category']) or die(mysql_error());
                            ?>
                        <tr height="20" valign="top" class="<?= $class1 ?>"  onmouseover="this.className='table_item_mouseover'" 
                            onmouseout="this.className='<?= $class1 ?>'">
                            <td align="center"><?= $no ?></td><td><?= $a['name_eng'] ?></td><td> <?= mysql_result($get_cat, 0, "name_eng") ?></td>
                            <td align="center"><?= convertYesNo($a['is_publish']) ?></td>
                            <td align="center"><a href="subcat_edit.php?id=<?= $id ?>"><img src="images/icons/edit.gif" border="0" /></a>  <a href="subcat_delete.php?id=<?= $id ?>"><img src="images/icons/delete.gif" border="0" /></a></td>								
                        </tr>
                        <?
                        $no++;
                    }
                }
                ?>

            </table>

            <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
                <table>
                    <tr>
                        <td colspan="3"><strong>Add New Sub Category</strong></td>
                    </tr>
                    <?
                    $get_cat = mysql_query("select * from os_category order by id asc");
                    ?>
                    <tr>
                        <Td width="13%">Main Category</Td><td>:</td>
                        <td width="87%">
                            <select name="main_cat" id="main_cat" class="text">
                                <?
                                while ($c = mysql_fetch_array($get_cat, MYSQL_ASSOC)) {
                                    ?>
                                    <option value="<?= $c['id'] ?>">
                                        <?= $c['name_eng'] ?>
                                    </option>
                                <? } ?>
                            </select>
                        </td>
                    </tr>
                    <tr valign="top">
                        <td>Category name</td><td>:</td><td><input class="text" type="text" name="name_eng" size="30" id="name_eng" onBlur="checkExist(); return false;">
                            <div id="divCatName"></div>
                        </td>
                    </tr>
                    <tr>
                        <td>Display</td><td>:</td><td>
                            <select name="is_publish">
                                <option value="1">Yes</option>
                                <option value="0">No</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3"><input type="submit" value="Submit" class="button" name="btnsubmit"></td>
                    </tr>
                </table>
            </form>

        </td>
    </tr>


</table>
<? require("required/footer.php"); ?>

